﻿using Cocos2D;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using TestTiger.Screens;

namespace TestTiger
{
    public class AppDelegate : CCApplication
    {
#if WP8
        public AppDelegate(Game game, GraphicsDeviceManager graphics)
            : base(game, graphics)
        {
            s_pSharedApplication = this;
            CCDrawManager.InitializeDisplay(game, graphics, DisplayOrientation.LandscapeRight | DisplayOrientation.LandscapeLeft);

            graphics.PreferMultiSampling = false;
        } 
#else
        public AppDelegate(PhoneApplicationPage game, SharedGraphicsDeviceManager graphics)
            : base(game, graphics)
        { 
            s_pSharedApplication = this;
            //CCDrawManager.InitializeDisplay(game, graphics, DisplayOrientation.LandscapeRight | DisplayOrientation.LandscapeLeft);
        }
#endif

        public override void Initialize()
        {
            base.Initialize();
        }

        public override bool ApplicationDidFinishLaunching()
        {
            CCDirector.SharedDirector.SetOpenGlView();
            CCDrawManager.SetDesignResolutionSize(800, 480, CCResolutionPolicy.ShowAll);

            //CCDirector.SharedDirector.DisplayStats = true;
            CCDirector.SharedDirector.AnimationInterval = 1f / 30;
            CCDirector.SharedDirector.RunWithScene(new TigerScreen());
            return base.ApplicationDidFinishLaunching();
        }

        public override void ApplicationDidEnterBackground()
        {
#if WP8
            SoundEngine.SoundEngine.sharedEngine().StopEngine();
#endif
            CCDirector.SharedDirector.Pause();

            base.ApplicationDidEnterBackground();
        }
        public override void ApplicationWillEnterForeground()
        {
#if WP8
            SoundEngine.SoundEngine.sharedEngine().StartEngine();
            var scene = CCDirector.SharedDirector.RunningScene as SceneMain;
            if (scene != null)
            {
                scene.ShowActivity();
            }
#endif
#if WP7
            HttpHelper.GetCoin();
#endif
            CCDirector.SharedDirector.Resume();

            //var sceneMain = CCDirector.SharedDirector.RunningScene as SceneMain;
            //if (sceneMain != null)
            //    sceneMain.showPlayerName(PlayerData.Instance.PlayerName);
            //var sceneMain1 = CCDirector.SharedDirector.RunningScene as SceneActorInfo;
            //if (sceneMain1 != null)
            //    sceneMain1.ShowActorName();
            base.ApplicationWillEnterForeground();
        }
    }
}